﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Data.Dynamic;
using EPiServer.Data;
using EPiServer.Personalization.VisitorGroups;
using System.ComponentModel.DataAnnotations;
using EPiServer.Web.Mvc.VisitorGroups;
using EPiServer.Security;
using System.Web.Security;

namespace CriteriaPack.RoleCriterion
{
    /// <summary>
    /// Model class for use by the RoleCriterion criterion for
    /// Visitor Groups. Stores a role name and a compare condition.
    /// </summary>
    public class RoleModel : CriterionModelBase
    {
        public EPiServer.Data.Identity Id { get; set; }

       
        [DojoWidget(
            SelectionFactoryType = typeof(EnumSelectionFactory),
            LabelTranslationKey = "/shell/cms/visitorgroups/criteria/role/comparecondition",
            AdditionalOptions = "{ selectOnClick: true }"),
            Required]
        public RoleCompareCondition Condition { get; set; }

        [DojoWidget(
            SelectionFactoryType= typeof(RoleSelectionFactory),
            LabelTranslationKey = "/shell/cms/visitorgroups/criteria/role/rolename",
            AdditionalOptions = "{ selectOnClick: true }"),
            Required]
        public string RoleName { get; set; }


        public override ICriterionModel Copy()
        {
            return base.ShallowCopy();
        }
    }


    public class RoleSelectionFactory : ISelectionFactory
    {
        #region ISelectionFactory Members

        public IEnumerable<System.Web.Mvc.SelectListItem> GetSelectListItems(Type property)
        {
            //Return list of Roles
            //VirtualRoles.GetAllRoles()
            return RoleProviderUtility.GetAllRolesWithProviders().Select(ri => ri.RoleName).Union(VirtualRoles.GetAllRoles()).Select(s => new System.Web.Mvc.SelectListItem(){ Text=s, Value=s});
        }

        #endregion
    }

    /// <summary>
    /// Enum representing compare conditions for booleans
    /// </summary>
    public enum RoleCompareCondition
    {
        Equal,
        NotEqual
    }
}